package com.example.demo.controller;

import com.example.demo.entity.RoleEntity;
import com.example.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping(value = "/{id}")
    public RoleEntity getRoleById(@PathVariable int id) {
        return roleService.getRoleById(id);
    }

    @DeleteMapping(value = "/{id}")
    public int deleteRoleById(@PathVariable int id) {
        return roleService.deleteRoleById(id);
    }

    //    http://localhost:8081/user/list
    @GetMapping(value = "/list")
    public List<RoleEntity> list() {
        return roleService.list();
    }

    //  新增用户
    // POST http://localhost:8081/user
    @PostMapping
    public RoleEntity add(@RequestBody RoleEntity user) {
        int id = roleService.addRole(user);
        return roleService.getRoleById(id);
    }

    @PutMapping
    public RoleEntity update(@RequestBody RoleEntity user) {

        // TODO 更新数据

        return roleService.getRoleById(user.getRoleId());
    }

}
